﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Forms;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.AnalysisTools;

namespace SPAS
{
    #region 季旻编写
    static class GNSS
    {
        /// <summary>
        /// 根据高度和截止高度角，计算缓冲区大小
        /// </summary>
        /// <param name="featureClass"></param>
        /// <param name="heightFieldName"></param>
        /// <param name="bufferFieldName"></param>
        /// <param name="degree"></param>
        static public void ComputeBufferDist(IFeatureClass featureClass, string heightFieldName, string bufferFieldName, double degree = 15)
        {
            double buildHeight;
            double r;
            double pi = Math.PI;

            // 将要素类转换为dataset
            IDataset dataset = featureClass as IDataset;
            // 获得工作空间
            IWorkspace workSpace = dataset.Workspace;
            // 获得编辑状态
            IWorkspaceEdit wsedit = workSpace as IWorkspaceEdit;
            wsedit.StartEditing(true);
            wsedit.StartEditOperation();

            IFeatureCursor featureCursor;
            IQueryFilter filter = new QueryFilterClass();

            filter.WhereClause = heightFieldName + ">0";

            featureCursor = featureClass.Search(filter, false);

            IFeature feature = featureCursor.NextFeature();
            int idxOfHeight = featureCursor.FindField(heightFieldName);
            int idxOfBuffer = featureCursor.FindField(bufferFieldName);

            while (feature != null)
            {
                buildHeight = Convert.ToInt32(feature.get_Value(idxOfHeight));

                r = Math.Tan(degree / 180 * pi) * buildHeight;

                feature.set_Value(idxOfBuffer, r);
                feature.Store();
                feature = featureCursor.NextFeature();
            }

            // 停止编辑
            wsedit.StopEditing(true);
            wsedit.StartEditOperation();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(wsedit);
        }


        static public void GetGNSSFeatureClass(IFeatureClass featureClass, string outFilePath)
        {
            Geoprocessor gp = new Geoprocessor();
            gp.OverwriteOutput = true;
            IGeoProcessorResult result = null;

            // 执行缓冲区分析
            string tempBufferPath = Utils.GetTempFilePath();
            tempBufferPath = System.IO.Path.ChangeExtension(tempBufferPath, ".shp");
            ESRI.ArcGIS.AnalysisTools.Buffer buffer = new ESRI.ArcGIS.AnalysisTools.Buffer();
            buffer.in_features = featureClass;
            buffer.out_feature_class = tempBufferPath;
            buffer.buffer_distance_or_field = "BufferDist";
            buffer.dissolve_option = "ALL";
            result = (IGeoProcessorResult)gp.Execute(buffer, null);
            if (result.Status != esriJobStatus.esriJobSucceeded)
                MessageBox.Show("缓冲区生成失败！");

            // 求交
            IFeatureLayer bufferFeature = Utils.LoadShapeFile(tempBufferPath) as IFeatureLayer;
            string tempIntersectPath = Utils.GetTempFilePath();
            tempIntersectPath = System.IO.Path.ChangeExtension(tempIntersectPath, ".shp");
            ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect();
            intersect.in_features = tempBufferPath + ";" + (featureClass as IDataset).Workspace.PathName + "\\" + (featureClass as IDataset).Name + ".shp";
            intersect.out_feature_class = tempIntersectPath;
            intersect.join_attributes = "ALL";
            intersect.output_type = "INPUT";
            result = (IGeoProcessorResult)gp.Execute(intersect, null);
            if (result.Status != esriJobStatus.esriJobSucceeded)
                MessageBox.Show("求交失败！");

            // 擦除
            ESRI.ArcGIS.AnalysisTools.Erase erase = new ESRI.ArcGIS.AnalysisTools.Erase();

            erase.in_features = featureClass;
            IFeatureLayer intersectFeature = Utils.LoadShapeFile(tempIntersectPath) as IFeatureLayer;

            erase.erase_features = intersectFeature;   // 擦除图层
            erase.out_feature_class = outFilePath;     // 输出文件路径
            result = (IGeoProcessorResult)gp.Execute(erase, null);
            if (result.Status != esriJobStatus.esriJobSucceeded)
                MessageBox.Show("擦除失败！");
        }
    }
    #endregion
}
